import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hm_show_flutter/api/service/user_service.dart';
import 'package:hm_show_flutter/entity/member_entity.dart';
import 'package:hm_show_flutter/widgets/cached_image.dart';
import 'package:hm_show_flutter/widgets/empty_view.dart';
import 'package:hm_show_flutter/widgets/loading_dialog.dart';
import 'package:url_launcher/url_launcher.dart';

///成员列表
class MemberListPage extends StatefulWidget {
  @override
  _MemberListPageState createState() => _MemberListPageState();
}

class _MemberListPageState extends State<MemberListPage> {
  UserService _service = new UserService();
  List<MemberDataList> _list;

  bool isLoading;

  @override
  void initState() {
    super.initState();
    getPageData();
  }

  void getPageData() {
    isLoading = true;
    _service.getMemberList((success) {
      setState(() {
        isLoading = false;
        MemberData data = success;
        _list = data.xList;
      });
    }, onFail: (fail) {
      setState(() {
        isLoading = false;
      });
      Navigator.pop(context);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
          child: AppBar(
            iconTheme: IconThemeData(color: Colors.white),
            automaticallyImplyLeading: true,
            centerTitle: true,
            title: Text(
              "成员列表",
              style: TextStyle(color: Colors.white, fontSize: 16.0),
            ),
          ),
          preferredSize: Size.fromHeight(ScreenUtil.instance.height * 0.036)),
      body: _getContentView(),
    );
  }

  Widget _getContentView() {
    if (isLoading) {
      return LoadingDialog();
    } else if (_list == null || _list.isEmpty) {
      return EmptyView(
        onRetry: () {
          getPageData();
        },
      );
    } else {
      return Container(
        child: SingleChildScrollView(
          child: Container(
            child: ListView.builder(
                shrinkWrap: true,
                itemCount: _list.length,
                physics: NeverScrollableScrollPhysics(),
                itemBuilder: (BuildContext context, int index) {
                  return _getViewItem(context, _list[index]);
                }),
          ),
        ),
      );
    }
  }

  Widget _getViewItem(BuildContext context, MemberDataList entity) {
    return Container(
      child: InkWell(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Container(
              padding: EdgeInsets.all(10),
              alignment: Alignment.centerLeft,
              color: Colors.grey[100],
              child: entity.subtitle == null? Container():Text(
                entity.subtitle,
                style: TextStyle(color: Colors.black54, fontSize: 14.0),
              ),
            ),
            ListView.builder(
                shrinkWrap: true,
                itemCount: entity.xList.length,
                physics: NeverScrollableScrollPhysics(),
                itemBuilder: (BuildContext context, int index) {
                  if (entity.xList == null) {
                    return Container();
                  }

                  return _getMemberItem(
                      context, entity.xList[index]); //返回item 布局
                }),
          ],
        ),
      ),
    );
  }

  Widget _getMemberItem(BuildContext context, MemberDataListList entity) {
    return Container(
      child: InkWell(
          child: Container(
        color: Colors.white,
        padding: EdgeInsets.all(8),
        child: Row(
          children: <Widget>[
            CachedImageView(60, 60, entity.url),
            Padding(padding: EdgeInsets.all(5)),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: <Widget>[
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Container(
                      child: entity.name == null? Container():Text(
                        entity.name,
                        style: TextStyle(color: Colors.black, fontSize: 14.0),
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.only(left: 5),
                      child: entity.titleStr == null? Container():Text(
                        entity.titleStr,
                        style: TextStyle(color: Colors.black, fontSize: 14.0),
                      ),
                    ),
                  ],
                ),
                Padding(padding: EdgeInsets.all(5)),
                entity.mobile == null
                    ? Container()
                    : Container(
                        child: Text(
                          entity.mobile,
                          style:
                              TextStyle(color: Colors.black54, fontSize: 14.0),
                        ),
                      ),
              ],
            ),
            Expanded(child: Container()),
            entity.mobile == null
                ? Container()
                : GestureDetector(
                    onTap: () {
                      if (entity.mobile != null) {
                        _launchURL(entity.mobile);
                      }
                    },
                    child: Container(
                      color: Colors.white,
                      padding: EdgeInsets.all(10),
                      alignment: Alignment.centerLeft,
                      child: Icon(Icons.phone),
                    ),
                  ),
          ],
        ),
      )),
    );
  }

  void _launchURL(String mobile) async {
    String url = 'tel:' + mobile;
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      print('Could not launch $url');
    }
  }
}
